<?php
/**
 * Created by PhpStorm.
 * User: 1
 * Date: 2018/3/23
 * Time: 15:23
 */

namespace app\api\model;
use think\Model;
use think\Log;
use think\Db;
use app\common\lib\exception\ApiException;

class TeacherStopLesson extends Model
{
    /**
     * @教师停课
     * @param t_id int 教师id 必传
     * @param les_id int 课程详情id 必传
     * @param t_stop_content string 停课原因 必传
     * @author 巫玉梅 <1120504614@qq.com>
     * @return ApiException|array|string|\think\response\Json
     */
    public function insertStopLes(){
        $data = $_POST;

        if(empty($data)){
           throw new ApiException('数据不合法',404);
        }

        // 课程id int 必传
        if(empty($data['les_id'])){
            throw new ApiException('les_id is not found',404);
        }

        // 教师id int 必传
        if(empty($data['t_id'])){
            throw new ApiException('t_id is not found',404);
        }

        // 结课原因 string 必传
        if(empty($data['t_stop_content'])){
            throw new ApiException('t_stop_content is not found',404);
        }

        try{

            $info = Db::name("teacher_stop_lesson")
                ->where('t_id',$data['t_id'])
                ->where('les_id',$data['les_id'])
                ->where('stu_stop_id','')
                ->where('t_stop_status',0)
                ->find();

            if(empty($info)){

                $obj = new TeacherStopLesson($data);
                $rec = $obj->allowField(true)->save();

                if($rec){
                    return show(config('code.success'),'申请成功，请等待审核');
                }else{
                    return show(config('code.error'),'申请失败');
                }
            }else{
                return show(config('code.error'),'结课请求失败，您已提交，勿须重复操作，请等待审核');
            }
        }catch(\Exception $e){
            Log::write($e->getMessage());
            throw new ApiException($e->getMessage(),500);
        }
    }

    /**
     * @教师选学生停课
     * @param t_id int 教师id 必传
     * @param stu_stop_id int 学生id 必传
     * @param les_id int 课程详情id 必传
     * @param t_stop_content string 停课原因 必传
     * @author 巫玉梅 <1120504614@qq.com>
     * @return ApiException|array|string|\think\response\Json
     */
    public function studentStopByTeacher(){
        $data = $_POST;

        if(empty($data)){
           throw new ApiException('数据不合法',404);
        }
        // 课程id int 必传
        if(empty($data['les_id'])){
            throw new ApiException('les_id is not found',404);
        }
        // 教师id int 必传
        if(empty($data['t_id'])){
            throw new ApiException('t_id is not found',404);
        }
        // 学生student_id  int 必传
        if(empty($data['stu_stop_id'])){
            throw new ApiException('stu_stop_id is not found',404);
        }
        // 停课原因 string 必传
        if(empty($data['t_stop_content'])){
            throw new ApiException('t_stop_content is not found',404);
        }
        try{
            $info = Db::name("teacher_stop_lesson")
                ->where('t_id',$data['t_id'])
                ->where('les_id',$data['les_id'])
                ->where('stu_stop_id',$data['stu_stop_id'])
                ->find();

            if(!empty($info)){
                if($info['t_stop_status'] == 0){
                    return show(config('code.error'),'您已提交停课申请，勿须重复操作');
                }elseif($info['t_stop_status'] == 2){
                    return show(config('code.error'),'该学生已申请停课，勿须重复操作');
                }else{
                    return show(config('code.error'),'您提交的停课记录已审核通过，勿须重复操作');
                }
            }else{
                $obj = new TeacherStopLesson($data);
                $rec = $obj->allowField(true)->save();

                if($rec){
                    return show(config('code.success'),'申请成功，请等待审核');
                }else{
                    return show(config('code.error'),'申请失败');
                }
            }

        }catch(\Exception $e){
            Log::write($e->getMessage());
            throw new ApiException($e->getMessage(),500);
        }
    }



}